import 'package:color_dart/color_dart.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/jsonserialize/group/data.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/home/widgets/top_search.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';

class ListItem extends StatelessWidget {
  final Group item;
  const ListItem({Key key, this.item}) : super(key: key);

  void apply() async {
    var res = await G.req.group.apply(groupId: '${item.id}');
    if (res.data['status'] == 1) {
      G.toast("申请已发送");
    }
  }

  @override
  Widget build(BuildContext context) {
    double screenW = G.screenWidth();
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Container(
                margin: EdgeInsets.only(right: 10),
                width: 60,
                height: 60,
                decoration: BoxDecoration(
                    shape: BoxShape.rectangle,
                    color: rgba(255, 255, 255, 1),
                    borderRadius: BorderRadius.all(Radius.circular(50)),
                    border:
                        Border.all(color: rgba(221, 221, 221, 1), width: 1)),
                child: SizedBox(
                  child: Image.network(
                    '${item.logo}',
                    width: 54,
                    height: 54,
                    fit: BoxFit.contain,
                  ),
                ),
              ),
              Container(
                  width: screenW * 0.5,
                  child: Text(
                    item.name,
                    style: TextStyle(
                        color: rgba(51, 51, 51, 1),
                        fontSize: 16,
                        fontWeight: FontWeight.w500),
                    overflow: TextOverflow.ellipsis,
                    maxLines: 1,
                  )),
            ],
          ),
          GestureDetector(
            onTap: () => {apply()},
            child: Container(
              padding: EdgeInsets.only(left: 16, right: 16, top: 2, bottom: 2),
              decoration: BoxDecoration(
                  shape: BoxShape.rectangle,
                  color: rgba(255, 255, 255, 1),
                  borderRadius: BorderRadius.all(Radius.circular(13)),
                  border: Border.all(color: rgba(47, 128, 250, 1), width: 1)),
              child: Text(
                '加入',
                style: TextStyle(color: rgba(47, 128, 250, 1), fontSize: 13),
              ),
            ),
          )
        ],
      ),
    );
  }
}

class GroupList extends StatefulWidget {
  const GroupList({Key key}) : super(key: key);

  @override
  _GroupListState createState() => _GroupListState();
}

class _GroupListState extends State<GroupList> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('小组列表')),
      body: Container(
        color: rgba(255, 255, 255, 1),
        child: Column(
          children: [
            Container(
              child: TopSearchbar(),
            ),
            Column(
              children: listData.map((item) => ListItem(item: item)).toList(),
            )
          ],
        ),
      ),
    );
  }

  List<Group> listData = [];

  @override
  void initState() {
    super.initState();
    this.getData();
  }

  void getData() async {
    var res = await G.req.group.grouplist();
    print(res.data['data']);
    res.data['data'].forEach((e) {
      Group list = Group.fromJson(e);
      listData.add(list);
    });

    this.setState(() {});
  }
}
